﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PayloadTracker
{
    public partial class DirectionalArrow : Control
    {
        double m_Course = 0.0f;
        Color m_Color = Color.Black;
        public DirectionalArrow()
        {
            SetDirection(180);
            InitializeComponent();
            this.Resize += new EventHandler(DirectionalArrow_Resize);
            this.DoubleBuffered = true;


        }

        void DirectionalArrow_Resize(object sender, EventArgs e)
        {
            this.Invalidate();
        }

        public void SetDirection(float Course)
        {
            if(Course < 0 || Course > 360)
            {
                Logger.LOG.Error("Invalid course given!");
                return;
            }

            m_Course = APRS.Tools.DegreesToRadians(Course);

            this.Invalidate();
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            pe.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

            pe.Graphics.DrawEllipse(new System.Drawing.Pen(m_Color, 2), new Rectangle(1, 1, this.Width-2, this.Height-2));

            Pen p = new Pen(Color.Black, 5);

            p.StartCap = LineCap.Round;
            p.EndCap = LineCap.ArrowAnchor;

            
            float radius = Width / 2 -1;

            float originx = (((float)Width) / 2);
            float originy = (((float)Height) / 2);

            float xd = ((float)Math.Sin(m_Course)) * radius;
            float yd = -((float)Math.Cos(m_Course)) * radius;

            pe.Graphics.DrawLine(p, originx,originy,xd+originx, yd+originy);
            p.Dispose();

            base.OnPaint(pe);
        }


    }
}
